package com.zxp.zxpsmarthomeapk.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.qmuiteam.qmui.widget.QMUITopBar;
import com.zxp.zxpsmarthomeapk.R;
import com.zxp.zxpsmarthomeapk.util.AnimUtils;
import com.zxp.zxpsmarthomeapk.view.activity.RoomDeviceActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class RoomFragment extends Fragment {

    @BindView(R.id.topbar_room)
    QMUITopBar topbar_room;
    @BindView(R.id.cv_livingroom)
    CardView cv_livingroom;
    @BindView(R.id.cv_diningroom)
    CardView cv_diningroom;
    @BindView(R.id.cv_kitchen)
    CardView cv_kitchen;
    @BindView(R.id.cv_bedroom)
    CardView cv_bedroom;
    @BindView(R.id.cv_bathroom)
    CardView cv_bathroom;
    @BindView(R.id.cv_playroom)
    CardView cv_playroom;
    @BindView(R.id.cv_bookroom)
    CardView cv_bookroom;
    @BindView(R.id.cv_childrenroom)
    CardView cv_childrenroom;

    @BindView(R.id.tv_livingroom_title)
    TextView tv_livingroom_title;
    @BindView(R.id.tv_diningroom_title)
    TextView tv_diningroom_title;
    @BindView(R.id.tv_kitchen_title)
    TextView tv_kitchen_title;
    @BindView(R.id.tv_bedroom_title)
    TextView tv_bedroom_title;
    @BindView(R.id.tv_bathroom_title)
    TextView tv_bathroom_title;
    @BindView(R.id.tv_playroom_title)
    TextView tv_playroom_title;
    @BindView(R.id.tv_bookroom_title)
    TextView tv_bookroom_title;
    @BindView(R.id.tv_childrenroom_title)
    TextView tv_childrenroom_title;


    private static final int MSG_INIT_ANIM = 0;
    private String[] room_name = {"主卧室", "餐厅", "厨房", "客房",
            "浴室", "家庭影院", "书房", "儿童房",};

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_INIT_ANIM:
                    playViewAnim();
                    break;
            }
        }
    };

    public RoomFragment() {
        // Required empty public constructor
    }

    private void playViewAnim() {
        AnimUtils.playViewAnim(getActivity(), cv_livingroom, R.anim.left_push_in);
        AnimUtils.playViewAnim(getActivity(), cv_diningroom, R.anim.right_push_in);
        AnimUtils.playViewAnim(getActivity(), cv_kitchen, R.anim.right_push_in, 0, 200);
        AnimUtils.playViewAnim(getActivity(), cv_bedroom, R.anim.top_down_alpha_in, 0, 300);
    }

    @OnClick({R.id.cv_livingroom, R.id.cv_diningroom, R.id.cv_kitchen, R.id.cv_bedroom, R.id.cv_bathroom, R.id.cv_playroom, R.id.cv_bookroom, R.id.cv_childrenroom})
    void goToAnotherActivity(View view) {
        String msg = null;
        switch (view.getId()) {
            case R.id.cv_livingroom:
                msg = room_name[0];
                break;
            case R.id.cv_diningroom:
                msg = room_name[1];
                break;
            case R.id.cv_kitchen:
                msg = room_name[2];
                break;
            case R.id.cv_bedroom:
                msg = room_name[3];
                break;
            case R.id.cv_bathroom:
                msg = room_name[4];
                break;
            case R.id.cv_playroom:
                msg = room_name[5];
                break;
            case R.id.cv_bookroom:
                msg = room_name[6];
                break;
            case R.id.cv_childrenroom:
                msg = room_name[7];
                break;
            default:
                break;
        }
        Bundle bundle = new Bundle();
        bundle.putString("msg", msg);
        Intent intent = new Intent(getActivity(), RoomDeviceActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_room, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        tv_livingroom_title.setText(room_name[0]);
        tv_diningroom_title.setText(room_name[1]);
        tv_kitchen_title.setText(room_name[2]);
        tv_bedroom_title.setText(room_name[3]);
        tv_bathroom_title.setText(room_name[4]);
        tv_playroom_title.setText(room_name[5]);
        tv_bookroom_title.setText(room_name[6]);
        tv_childrenroom_title.setText(room_name[7]);

        topbar_room.addLeftTextButton("管理", R.id.topbar_left_back_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "管理recycleView", Toast.LENGTH_SHORT).show();
            }
        });

        topbar_room.addRightImageButton(R.drawable.ic_settings_black_24dp, R.id.topbar_right_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "显示设置", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mHandler.sendEmptyMessageDelayed(MSG_INIT_ANIM, 200);
    }
}
